package com.homelink.android.explorerup.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.homelink.android.explorerup.R;

public class NumOfImageView extends ImageView {
	public static String TAG = "NumOfImageView";
	public static final int FONT_OFFSET_X = 20;
	public static final int FONT_OFFSET_Y = 20;
	
	private int mNumber = 0;
	private Context mContext;
	private String mSuffix;
	private boolean mIsUnkown = false;
	private Paint mPaint = null;
	
	

	public SelectMode mSelectMode = SelectMode.NORMAL;

	public static enum SelectMode {
		SELECTED, UNSELECTED, NORMAL
	};

	public void reset() {
		mSuffix = null;
		mIsUnkown = false;
		mNumber = 0;
		mSelectMode = SelectMode.NORMAL;
		this.setImageResource(R.drawable.file_96);
	}

	public void setIsUnkownType(boolean isUnkown) {
		mIsUnkown = isUnkown;
	}

	public void setSuffix(String name) {
		mSuffix = name;
	}

	public SelectMode getSelectMode() {
		return mSelectMode;
	}

	public void setSelectMode(SelectMode mode) {
		mSelectMode = mode;
	}

	public NumOfImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setStrokeWidth(5);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setTextAlign(Align.LEFT);
		
	}

	public void setNumber(int num) {
		mNumber = num;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		
		//Log.d(TAG, "onDraw select mode : " + this.mSelectMode);
		//TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG
		//		| Paint.DEV_KERN_TEXT_FLAG);
		
		super.onDraw(canvas);
		Rect rect = canvas.getClipBounds();
		if (mNumber >= 0) {
			FontMetrics fontMetrics = mPaint.getFontMetrics();
			String str = String.valueOf(mNumber);
			/*
			 * Rect bounds = new Rect();
			 * 
			 * float[] widths = new float[str.length()]; int count =
			 * mPaint.getTextWidths(str,0,str.length(),widths); float w =
			 * mPaint.measureText(str, 0, str.length());
			 * mPaint.getTextBounds(str, 0, str.length(), bounds);
			 * bounds.offsetTo(20, 20); mPaint.setColor(0xFF88FF88);
			 * canvas.drawRect(bounds, mPaint);
			 */
			
			mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
			
			mPaint.setTextSize(20);
			
			int blue = Color.rgb(0, 150, 219);
			mPaint.setColor(blue);
			float fontTotalHeigth = fontMetrics.bottom - fontMetrics.top;
			float offY = fontTotalHeigth / 2 - fontMetrics.bottom;
			float newY = rect.bottom - offY - FONT_OFFSET_Y;
			canvas.drawText(str, 9, newY, mPaint);
		}

		mPaint.setTextSize(20);
		mPaint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
		mPaint.setColor(Color.rgb(0, 150, 219));
		FontMetrics fontMetrics = mPaint.getFontMetrics();
		float fontTotalHeigth = fontMetrics.bottom - fontMetrics.top;
		float offY = fontTotalHeigth / 2 - fontMetrics.bottom;
		float newY = rect.top + offY + FONT_OFFSET_Y;

		if (mIsUnkown && mSuffix != null && mSuffix.length() > 0) {
			canvas.drawText(mSuffix, 9, newY, mPaint);
		}

		if (mSelectMode == SelectMode.SELECTED) {
			Bitmap bitmap = BitmapFactory
					.decodeResource(
							mContext.getResources(),R.drawable.check_32);
			canvas.drawBitmap(bitmap, rect.right - bitmap.getWidth() - 2,
					rect.bottom - bitmap.getHeight() - 2, null);
		}
		
		
	}

}
